package fr.upec.tisdtr.indoorlocation.location;

import android.util.Log;

public class PlanLocation {
	
	private static final String TAG = "PlanLocation";

	/* LEFT UP CORNER LOCATION ON MAP */
	public static double LEFT_TOP_LAT 		= 48.776956;
	public static double LEFT_TOP_LONG 		= 2.374575;
	
	/* RIGHT UP CORNER LOCATION ON MAP */
	public static double RIGHT_TOP_LAT		= 48.776973;
	public static double RIGHT_TOP_LONG		= 2.375154;
	
	/* LEFT DOWN CORNER LOCATION ON MAP */
	public static double LEFT_BOTTOM_LAT	= 48.776740;
	public static double LEFT_BOTTOM_LONG	= 2.374580;
	
	/* RIGHT DOWN CORNER LOCATION ON MAP */
	public static double RIGHT_BOTTOM_LAT	= 48.776758;
	public static double RIGHT_BOTTOM_LONG 	= 2.375171;
	
	
	private int parentWidth; 	// px
	private int parentHeight; 	// px
	
	public PlanLocation(int parentWidth, int parentHeight) {
		this.parentWidth = parentWidth - 2*30;
		this.parentHeight = parentHeight - 2*30;
	}
	
	
	/** compute rules : 
	 * 
	 * LAT 	<=> 	ORD
	 * LONG <=> 	ABS
	 * 	
	 * (LEFT_TOP_LAT, LEFT_TOP_LONG) 			--> (0, 0)
	 * (RIGHT_TOP_LAT, RIGHT_TOP_LONG) 			--> (parentWidth, 0)
	 * (LEFT_BOTTOM_LAT, LEFT_BOTTOM_LONG) 		--> (0, parentHeight)
	 * (RIGHT_BOTTOM_LAT, RIGHT_BOTTOM_LONG) 	--> (parentWidth, parentHeight)
	 * 
	 *         A		 --- x -->		  B
	 *          -------            -------
	 *    |     |	  |			   |     |   
	 *  y |     |	  |			   |     |    
	 *    |     |	  |____________|	 |   
	 *    V     |						 |  
	 *          |						 |
	 *          --------------------------
	 *         D						  C
	 *   
	 *   A ( xa, ya )		xa ~= xd
	 *   B ( xb, yb )		xb ~= xc
	 *   C ( xc, yc )		ya ~= yb
	 *   D ( xd, yd )		yd ~= yc
	 *   
	 *   Dismiss angle between real building and plan.
	 *   So, mean coords is the best way for our computations.
	 */
	public MarkerPosition computeMarkerPosition(double longitude, double latitude) {
		double xa = (LEFT_TOP_LONG+LEFT_BOTTOM_LONG)/2;
		double xb = (RIGHT_TOP_LONG+RIGHT_BOTTOM_LONG)/2;
		double ya = (LEFT_TOP_LAT+RIGHT_TOP_LAT)/2;
		double yd = (LEFT_BOTTOM_LAT+RIGHT_BOTTOM_LAT)/2;
		
		int xPixels = 0, yPixels = 0;
		
		// if we are out of left border
		if(longitude <= xa) {
			Log.v(TAG, "out of left border : longitude="+longitude+", xa="+xa);
			xPixels = 0;
		}
		// if we are out of right border 
		else if(longitude >= xb) {
			Log.v(TAG, "out of right border : longitude="+longitude+", xb="+xb);
			xPixels = parentWidth;
		} 
		// we are between xa & xb
		else {
			Log.v(TAG, "between xa & xb");
			xPixels = (int) ( (parentWidth/(xb-xa))*longitude +  parentWidth - ((parentWidth*xb)/(xb-xa)) );
		}
		
		
		// if we are out of top border
		if(latitude >= ya) {
			Log.v(TAG, "out of top border : latitude="+latitude+", ya="+ya);
			yPixels = 0;
		}
		// if we are out of bottom border
		else if(latitude <= yd) {
			Log.v(TAG, "out of bottom border : latitude="+latitude+", yd="+yd);
			yPixels = parentHeight;
		}
		// we are between ya & yd
		else {
			Log.v(TAG, "between ya & yd");
			yPixels = (int) ( ((parentHeight/(yd-ya))*latitude + parentHeight - ((parentHeight*yd)/(yd-ya))) );
		}
		
		return new MarkerPosition(xPixels, yPixels);
	}
	
	
	  /////////////////
	 // INNER CLASS //
	/////////////////
	
	public class MarkerPosition {
		
		int posWidth; 	// px 
		int posHeight; 	// px
		
		public MarkerPosition(int posWidth, int posHeight) {
			this.posWidth = posWidth;
			this.posHeight = posHeight;
		}
		
		public int getPosWidth() {
			return posWidth;
		}
		
		public int getPosHeight() {
			return posHeight;
		}
		
		@Override
		public String toString() {
			return "MarkerPosition [posWidth=" + posWidth + ", posHeight=" + posHeight + "]";
		}
	}
	
}
